﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using DAL.DBHelpers;
namespace QuanLyKhachHang
{
    public partial class uc_GiaoDich : UserControl
    {
        
        public uc_GiaoDich()
        {
            InitializeComponent();
        }
        string id = string.Empty;
        public void loaddata()
        {
            DataTable dt = DBHelper.GetDataTableSP("");
            gdGD.DataSource = dt;
        }
        public void resetField()
        {
            txtGiaTri.Text = "";
            txtSL.Text = "";
            txtNgayGD.Text = "";
            txtMaSP.Text = "";
            txtMaKH.Text = "";
            cbTrangThai.Checked = false;

        }
        private void uc_GiaoDich_Load(object sender, EventArgs e)
        {
            loaddata();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
           
            List<SqlParameter> ls = new List<SqlParameter>();
    
            ls.Add(new SqlParameter("@", txtMaKH.Text));
            ls.Add(new SqlParameter("@", txtMaSP.Text));
            ls.Add(new SqlParameter("@", txtGiaTri.Text));
            ls.Add(new SqlParameter("@", txtNgayGD.Text));
            ls.Add(new SqlParameter("@", txtSL.Text));
           
            bool TrangThai;
            if (cbTrangThai.Checked) TrangThai = true;
            else TrangThai = false;
            ls.Add(new SqlParameter("@TrangThai", TrangThai));
            DBHelper.ExecuteNonQuery("", ls);
            loaddata();
            MessageBox.Show("Thêm Thành công !");
        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@", txtMaKH.Text));
            ls.Add(new SqlParameter("@", txtMaSP.Text));
            ls.Add(new SqlParameter("@", txtNgayGD.Text));
            ls.Add(new SqlParameter("@", txtGiaTri.Text));
            ls.Add(new SqlParameter("@", txtSL.Text));
          
          
            Boolean a;
            if (cbTrangThai.Checked) a = true;
            else a = false;
            ls.Add(new SqlParameter("@TrangThai", a));
            DBHelper.ExecuteNonQuery("", ls);
            loaddata();
            resetField();
            MessageBox.Show("Cập nhật thành công!");
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@",id));
            DBHelper.ExecuteNonQuery("", ls);
            resetField();
            loaddata();
            MessageBox.Show("Xóa thành công !");
        }

        private void BtnExport_Click(object sender, EventArgs e)
        {
            
        }

        private void gdGD_Click(object sender, EventArgs e)
        {
          
            id = gdvNV.GetFocusedDisplayText();

            DataTable dt = new DataTable();
            List<SqlParameter> l = new List<SqlParameter>();
            l.Add(new SqlParameter("@", id));
            dt = DBHelper.GetDataTableSP("", l);
            
            txtGiaTri.Text = gdvNV.GetFocusedRowCellDisplayText(col_GiaTri);
            txtMaKH.Text = gdvNV.GetFocusedRowCellDisplayText(col_MaHK);
            txtMaSP.Text = gdvNV.GetFocusedRowCellDisplayText(col_MaSP);
            txtSL.Text = gdvNV.GetFocusedRowCellDisplayText(col_SoLuong);
            
           
            string tt = gdvNV.GetFocusedRowCellDisplayText(col_TrangThai);
            if (tt == "Checked") cbTrangThai.Checked = true;
            else
            {
                cbTrangThai.Checked = false;
            }
            txtNgayGD.Text = gdvNV.GetFocusedRowCellDisplayText(col_NgayGiaoDich);
        }

    }
}
